package org.example.blogger.controller;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.example.blogger.constant.Constants;
import org.example.blogger.model.BlogInfo;
import org.example.blogger.model.Result;
import org.example.blogger.service.BlogService;
import org.example.blogger.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RequestMapping("/blog")
@RestController
public class BlogController {
    @Autowired
    private BlogService blogService;

    @RequestMapping("/getList")
    public List<BlogInfo> getList(){
        log.info("获取博客列表");
        return blogService.getList();
    }
    @RequestMapping("/getBlogDetail")
    public BlogInfo getBlogDetail(Integer blogId,HttpServletRequest request){
        log.info("getBlogDetail,blogId:{}",blogId);
        BlogInfo blogDetail = blogService.getBlogDetail(blogId);
        //获取token
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        //从token中获取登录用户id
        Integer userId = JwtUtils.getIdByToken(token);
        //判断是否为登录用户
        if(userId !=null && userId == blogDetail.getUserId()){
            blogDetail.setIsLoginUesr(true);
        }else {
            blogDetail.setIsLoginUesr(false);
        }
        return blogDetail;
    }
    @RequestMapping("/add")
    public Result addBlog(@RequestBody BlogInfo blogInfo , HttpServletRequest request){
        log.info("添加博客,blogInfo:{}",blogInfo);
        if(!StringUtils.hasLength(blogInfo.getTitle()) || !StringUtils.hasLength(blogInfo.getContent())){
            return Result.fail("标题或内容不能为空");
        }
        //获取token
        String token = request.getHeader(Constants.REQUEST_HEADER_TOKEN);
        //从token中获取登录用户id
        Integer userId = JwtUtils.getIdByToken(token);
        blogInfo.setUserId(userId);
       if( !blogService.insertBlog(blogInfo)){
           return Result.fail("内部错误,请联系管理员");
       }
        return Result.success("");
    }
    @RequestMapping("/update")
    public  Result  update(BlogInfo blogInfo){
        log.info("更新博客,blogInfo:{}",blogInfo);
        if (blogInfo.getId() == null
                || !StringUtils.hasLength(blogInfo.getTitle())
                || !StringUtils.hasLength(blogInfo.getContent())) {
            return Result.fail("博客已删除或不存在");
        }
        blogService.update(blogInfo);
        return Result.success("");
    }
    @RequestMapping("/delete")
    public  Result delete(Integer blogId){
        log.info("博客删除 blogId:{}",blogId);
        if (blogId <=0 ) {
            return  Result.fail("博客已删除或不存在");
        }
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(1);
        if (blogService.update(blogInfo) != 1 ) {
            return Result.fail("内部错误,请联系管理员");
        }
        return Result.success("");
    }

}
